package com.sweets.look.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


import com.sweets.look.R;
import com.sweets.look.constant.Constants;
import com.sweets.look.util.CrashUtils;
import com.sweets.look.util.UploadUtils;
import com.sweets.look.util.UploadUtils.OnUploadListener;
import com.sweets.look.util.ViewUtils;

public final class CrashActivity extends Activity {
    private CrashUtils.EventListener eventListener;
    private Activity mActivity;
    private Context mContext;
    private Class restartActivityClass;

    public CrashActivity() {
        super();
    }


    public void onBtnClick(View v) {
        switch (v.getId()) {
            case R.id.crash_upload_btn: {
                UploadUtils v1 = new UploadUtils(this.mContext);
                v1.setOnUploadListener(new OnUploadListener() {
                    @Override
                    public void failed() {
                        ViewUtils.createInformativeDialog(CrashActivity.this.mActivity, CrashActivity.this
                                .mContext.getResources().getString(R.string.dialog_message), CrashActivity.this.mContext
                                .getResources().getString(R.string.code_500));
                    }

                    @Override
                    public void success() {
                        CrashActivity.this.findViewById(R.id.crash_upload_btn).setVisibility(View.GONE);
                        ViewUtils.createInformativeDialog(CrashActivity.this.mActivity, CrashActivity.this
                                .mContext.getResources().getString(R.string.dialog_message), CrashActivity.this.mContext
                                .getResources().getString(R.string.upload_completed));
                    }
                });
                v1.uploadInfo(Constants.URL_ERROR_API, CrashUtils.getErrorUploadMap(this.mContext,
                        this.getIntent()));
                break;
            }
            case R.id.crash_close_btn: {
                if (this.restartActivityClass != null) {
                    CrashUtils.restartApplicationWithIntent(((Activity) this), new Intent(((Context) this), this
                            .restartActivityClass), eventListener);
                } else {
                    CrashUtils.closeApplication(((Activity) this), eventListener);
                    return;
                }
            }
        }

        return;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = this;
        this.mActivity = this;
        this.setContentView(R.layout.crash_activity);
        this.restartActivityClass = CrashUtils.getRestartActivityClassFromIntent(this.getIntent());
        this.eventListener = CrashUtils.getEventListenerFromIntent(this.getIntent());
        if (this.restartActivityClass != null) {
            ((TextView) this.findViewById(R.id.crash_close_btn))
                    .setText(this.getResources().getString(R.string.crash_restart));
        }

        CrashUtils.getStackTraceFromIntent(this.getIntent());
        this.findViewById(R.id.crash_info).setVisibility(View.GONE);
    }
}

